{% load static %}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>码段添加页面</title>

  <link rel="stylesheet" href="{% static 'layui/css/layui.css' %}">
</head>
<body>

<div class="layui-container">
	<div class="layui-row">
		<div class="layui-col-md4">
			<form class="layui-form" style="margin-top: 20px;">
				{% csrf_token %}
				<input type="hidden" name="id" lay-verify="required" value={{NyCodeTypeSet.0.id}}  class="layui-input">

			  <div class="layui-form-item">
			    <label class="layui-form-label">类型名称</label>

			    <div class="layui-input-block">
			      <input type="text" name="title" lay-verify="required" value={{NyCodeTypeSet.0.title}}  class="layui-input">
			    </div>
			  </div>

<!--			  <div class="layui-form-item">-->
<!--				   <label class="layui-form-label">类型</label>-->
<!--			    <div class="layui-input-block">-->
<!--			      <select name="type" id='type' lay-verify="required" value={{CodeQueryset.0.type}}>-->
<!--			        {% if TypeQueryset %}-->
<!--					  {% for item in TypeQueryset %}-->
<!--					  		{% if item.title|stringformat:"s" == CodeQueryset.0.type|stringformat:"s" %}-->
<!--							<option value="{{ item.id }}" selected>-->
<!--								{{ item.title }}-->
<!--					  		</option>-->
<!--					  		{% else %}-->
<!--					  		<option value="{{ item.id }}">-->
<!--								{{ item.title }}-->
<!--					  		</option>-->
<!--					  		{% endif %}-->
<!--						{% endfor %}-->
<!--				  	{% endif %}-->
<!--			      </select>-->
<!--			    </div>-->
<!--			  </div>-->

			<div class="layui-form-item layui-form-text">
			 <label class="layui-form-label">备注</label>
			 <div class="layui-input-block">
			   <textarea name="remark" placeholder="请输入内容" class="layui-textarea" >{{NyCodeTypeSet.0.remark}}</textarea>
			 </div>
		   </div>

			  <div class="layui-form-item">
			    <div class="layui-input-block">
			      <button type="submit" class="layui-btn" lay-submit lay-filter="edit">立即修改</button>
<!--			      <button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
			    </div>
			  </div>
			</form>
		</div>
	</div>

</div>

<script src="{% static 'layui/layui.js' %}"></script>
<script src="{% static 'js/xm-select.js' %}"></script>

<script>
	const BaseUrl = "{{BaseUrl}}"
	layui.use(['form', 'jquery'],function() {
		let form = layui.form;
		let $ = layui.jquery;
		let layer = layui.layer;


		// 监听修改事件
    form.on('submit(edit)', function(data) {
		let field = data.field; // 获取表单字段值
		console.log(field);
        $.ajax({
            url: BaseUrl +'api/ny/code/type/'+field.id+'/',
            type: 'PUT',
            headers: {
                'X-CSRFToken': '{{ csrf_token }}'
            },
            data: data.field,
            success: function(result) {
				if(result.code===0){
						//关闭弹出层
					layer.msg('编辑成功！', { icon: 1 ,time:2});
					let index = parent.layer.getFrameIndex(window.name); // 先得到当前 iframe 层的索引
					parent.layer.close(index); // 再执行关闭
				}else{
						// 使用 Object.entries 获取键值对数组，并取出第一个键值对
						console.log(result.msg);
						const [[firstKey, firstValue]] = Object.entries(result.msg);
						console.log(firstKey,firstValue);
						layer.msg(firstKey+':'+firstValue,{ icon: 2 })
					}
            },
            error: function(xhr, status, error) {
				console.error("编辑失败:",xhr, status, error);
                layer.msg('编辑失败！', { icon: 2 });
            }
        });
		return false; // 阻止表单默认提交
    });

	})


</script>

</body>
</html>